home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 5 / BBS in a Box -Volume V (BBS in a Box) (April 1992).iso / Files / Bus / M / mathsubs.cpt / POLYFIT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-17  |  2.4 KB  |  72 lines  |  [TEXT/pZIP]

  1. #define    N    10
  2.  
  3. /*****************************************************************************
  4.  *                    power()                     *
  5.  *****************************************************************************
  6.  * DESCRIPTION:    Raises a real number to an integer power.             *
  7.  *                                         *
  8.  * SYNOPSIS:    double power(a, n);        Raise a to the nth power     *
  9.  *        double    a;                             *
  10.  *        int    n;                             *
  11.  *                                         *
  12.  * REVISIONS:    20 DEC 88 - RAC - Original code                     *
  13.  *****************************************************************************/
  14.  
  15. double power(a, n)
  16. double    a;
  17. int    n;
  18. {
  19.     double    rv;                /* Return value */
  20.  
  21.     rv = 1;
  22.     while (n--) rv *= a;
  23.     return rv;
  24.     }                        /* End power() */
  25. /*****************************************************************************
  26.  *                   polyfit()                     *
  27.  *****************************************************************************
  28.  * DESCRIPTION:    Finds the mean square regression curve (or line) for a set   *
  29.  *        of points, as shown on pp 172-3 of Burington's "Handbook     *
  30.  *        of Mathematical Tables and Formulas".                 *
  31.  *                                         *
  32.  * SYNOPSIS:    polyfit(n, data, k, A);                         *
  33.  *        int    n;            Degree of polynomial desired *
  34.  *        int    k;            Number of data points         *
  35.  *        double    *data;            Pointer to (2 by k) array of *
  36.  *                         (x, y) points             *
  37.  *        double    *A;            Pointer to result array         *
  38.  *                                         *
  39.  * REVISIONS:    19 DEC 88 - RAC - Original code                     *
  40.  *****************************************************************************/
  41.  
  42. polyfit(n, data, k, A)
  43. int    n;
  44. int    k;
  45. double    data[][2];
  46. double    A[N];
  47. {
  48.     int        i, j, row, col;            /* Local temps */
  49.     double    S, T;                /* See Burington, p. 173 */
  50.     double    ST[N][N+1];
  51.  
  52.     for (j=0; j<=(2*n); j++) {            /* Calculate all the S's */
  53.     if (j == 0) S = k;            /* S0 is a special case */
  54.     else {                    /* All others are normal */
  55.         S = 0;
  56.         for (i=0; i<k; i++)
  57.         S += power(data[i][0], j);
  58.         }
  59.     for (row=0; row<=n; row++)        /* Put S where it goes in */
  60.         for (col=0; col<=n; col++)        /*  the big array */
  61.         if (j == (row + col))
  62.             ST[row][col] = S;
  63.     }                    /* End 'calculate the S's */
  64.     for (j=0; j<=n; j++) {            /* Calculate all the T's and */
  65.     T = 0;                    /*  stick them in the big */
  66.     for (i=0; i<k; i++)            /*  array */
  67.         T += (power(data[i][0], j) * data[i][1]);
  68.     ST[j][n+1] = T;
  69.     }                    /* End 'calculate the T's */
  70.     simul(ST, A, n+1);                /* Go solve the equations */
  71.     }                        /* End polyfit() */
  72.